import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:work_tools/config/config.dart';
import 'package:work_tools/utils/cache_util.dart';
import 'package:work_tools/values/color.dart';
import 'package:work_tools/values/text_style.dart';
import 'package:work_tools/widget/sheets/add_whitelist_sheet/add_whitelist_widget.dart';
import 'excel_power_logic.dart';

class ExcelPowerPage extends StatelessWidget {
  final logic = Get.put(ExcelPowerLogic());
  final state = Get.find<ExcelPowerLogic>().state;

  ExcelPowerPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("VIN码白名单"),
        actions: [
          Padding(
            padding: const EdgeInsets.only(right: 20),
            child: IconButton(
              onPressed: () => addVin(context),
              icon: Icon(
                Icons.add,
                color: ColorsValue.white,
                size: 30,
              ),
            ),
          ),
        ],
      ),
      body: GetBuilder(
        init: logic,
        builder: (_) {
          return Column(
            children: [
              SizedBox(
                height: 60,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    const SizedBox(width: 40),
                    Expanded(
                      child: TextFormField(
                        focusNode: state.keyNode,
                        onChanged: logic.filterVines,
                        initialValue: state.filterKey,
                        keyboardType: TextInputType.text,
                        style: TStyleUtil.sc_4_14(ColorsValue.black2a),
                        decoration: InputDecoration(
                          contentPadding: EdgeInsets.zero,
                          border: const OutlineInputBorder(borderSide: BorderSide.none),
                          hintText: "请输入匹配的vin码（忽略大小写）",
                          counterText: "",
                          hintStyle: TStyleUtil.sc_4_14(ColorsValue.textGrey),
                        ),
                        maxLength: 30,
                      ),
                    ),
                  ],
                ),
              ),
              Expanded(child: ListView.separated(
                itemBuilder: itemBuilder,
                separatorBuilder: separatorBuilder,
                itemCount: state.filterVinesRes.length,
              )),
            ],
          );
        },
      ),
    );
  }

  Widget itemBuilder(BuildContext context, int index) {
    final vin = state.filterVinesRes[index];
    return Container(
      padding: const EdgeInsets.only(left: 40),
      child: Row(
        children: [
          Text(vin, style: TStyleUtil.sc_4_16(ColorsValue.black2a)),
          const Spacer(),
          IconButton(
            onPressed: () => deleteVinDialog(context, vin),// () => logic.deleteVin(vin),
            icon: Icon(Icons.delete, color: ColorsValue.hintRed),
          ),
          const SizedBox(width: 20),
        ],
      ),
    );
  }

  Widget separatorBuilder(BuildContext context, int index) {
    return const SizedBox(height: 40);
  }

  void deleteVinDialog(BuildContext context, String vin) async {
    // 弹出二次确认对话框
    final res = await showDialog<bool>(
      context: context,
      barrierDismissible: false, // 用户必须点击按钮才能关闭对话框
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('删除vin码'),
          content: Text('您确定要删除这个VIN码（$vin）吗？删除后，该vin码将不再具有白名单的权限'),
          actions: <Widget>[
            TextButton(
              onPressed: () => Navigator.of(context).pop(false),
              child: const Text('取消'),
            ),
            TextButton(
              onPressed: () => Navigator.of(context).pop(true),
              child: const Text('确定'),
            ),
          ],
        );
      },
    );
    if (true == res) {
      logic.deleteVin(vin);
    }
  }

  addVin(BuildContext ctx) async {
    final res = await AddWhitelistSheet.displaySheet(ctx, redisKey: AppConfig.vinRedisKey, redisValue: "");
    if (res == null) return;
    logic.addVin(res);
  }
}
